import {deleteRole, getRole, postRole, putRole, putRoleRes} from '@/base/api/RoleApi';
import {Rule} from '@/base/model/Rule';
 // import {Resource} from '@/base/model/Resource';
import {getResourceMeg, getResourceMegId} from '@/base/api/ResourceApi';
import {getRule, putRuleRes} from "@/base/api/RuleApi";

class Form {
  missionId = 0 ;
  mission = '';
  state = 0 ;
  developValue = 0 ;
  missionType = 0;
  // createBegin = '';
  // createEnd = '';
  // colourname = '';
  // date = [];
}

export class FormBloc {
  // form1 = new Form();
  // form2 = new Form();
  rule = new Rule();
  // resourceAll: Array<Resource> = [];
  // resourceItems: Array<Resource> = [];
  loading = false;
  // isShowRoleModal = false;
  result: any = [];
  itemsArr1: any = [];
  itemsArr11: any = [];

  itemsArr2: any = [];
  itemsArr22: any = [];
  load(pageIndex:number,pageSize:number,sortDirection) {
    return getRule({
        pageIndex,
        pageSize,
        sortDirection
      })
      .then((res) => {
        this.result = res.data;
        this.itemsArr1 = this.result.items.filter(x=>x.missionType === 1);
        this.itemsArr2 = this.result.items.filter(x=>x.missionType === 0);

        this.itemsArr1.forEach(
          x=>this.itemsArr11.push({value:[],data:this.itemsArr1.filter(j=>j.missionId === x.missionId)})
        );
        this.itemsArr11.forEach(x=>{
          if (x.data[0].state === 0) {
            x.value = []
          }
          else {
            x.value = [x.data[0].missionId]
          }
        });
        this.itemsArr2.forEach(
          x=>this.itemsArr22.push({value:[],data:this.itemsArr2.filter(j=>j.missionId === x.missionId)})
        );
        this.itemsArr22.forEach(x=> {
          if (x.data[0].state === 0) {
            x.value = []
          }
          else {
            x.value = [x.data[0].missionId]
          }
        });
        console.log(this.itemsArr11);
        return res;
      });
  }

  // loadReSource(id: number) {
  //   return getResourceMegId({id})
  //     .then((res) => {
  //       // @ts-ignore
  //       this.resourceItems = res.data;
  //       this.handleAlreadyItems();
  //       return res;
  //     });
  // }

  // getItemsArr() {
  //   const mainItems = this.resourceAll.filter(x => x.parentId === '0');
  //   console.log(mainItems);
  //   mainItems.forEach(x => {
  //     this.itemsArr.push({parent: x, value: [], data: this.resourceAll.filter(j => j.parentId === x.id)});
  //   });
  // }
  //
  // handleAlreadyItems() {
  //   this.itemsArr.forEach(x => {
  //     x.value = this.resourceItems.filter(j => j.parentId === x.parent.id).map(x => x.id);
  //   });
  // }
  //
  // save(id: number | '') {
  //   const entity = {
  //     name: this.role.name,
  //     description: this.role.description
  //     // res: this.itemsArr.map(x => x.value)
  //   };
  //   return id ? putRole(id, entity) : postRole(entity);
  // }
  //
  saveRes() {
    const entity = {
      res1: this.itemsArr11,
      res2: this.itemsArr22,
    };
    // console.log(entity);
    return putRuleRes(entity)
  }
  //
  // changeRoleModal() {
  //   this.isShowRoleModal = !this.isShowRoleModal;
  // }

}
